package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.rewardcoin.MallCommentResult;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2017/4/14.
 */

public class RewardCommentAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<MallCommentResult.MallComment> list;
    private JSONObject ary;

    public RewardCommentAdapter(Context context, List<MallCommentResult.MallComment> data) {
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try{
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_comment, parent, false);
                holder.rb = (RatingBar) convertView.findViewById(R.id.rb);
                holder.tvPublish = (TextView) convertView.findViewById(R.id.tvPublish);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if(null!=list.get(position)&&!"".equals(list.get(position))){
                MallCommentResult.MallComment data = list.get(position);
                if(null!=data.getStarlevel()&&!"".equals(data.getStarlevel())){
                    holder.rb.setRating(Float.parseFloat(data.getStarlevel()));
                }
                if(null!=data.getContent()&&!"".equals(data.getContent())){
                    holder.tvComment.setText(data.getContent());
                }else{
                    holder.tvComment.setText("");
                }
                if(null!=data.getCtime()&&!"".equals(data.getCtime())){
                    holder.tvTime.setText(data.getCtime());
                }else{
                    holder.tvTime.setText("");
                }
                try {
                    String username = "";
                    if (null != ary && !"".equals(ary)) {
                        if (null != data.getUserid() && !"".equals(data.getUserid())) {
                            username = ary.getString(data.getUserid());
                        }
                    }
                    if(!"".equals(username)){
                        holder.tvPublish.setText(username);
                    }else{
                        holder.tvPublish.setText("");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public void setUser(JSONObject ary) {
        this.ary = ary;
    }

    class ViewHolder{
        private RatingBar rb;
        private TextView tvPublish;
        private TextView tvTime;
        private TextView tvComment;
    }
}
